package com.huiminpay.merchant.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huiminpay.common.cache.domain.CommonErrorCode;
import com.huiminpay.common.cache.exception.BusinessCast;
import com.huiminpay.common.cache.exception.BusinessException;
import com.huiminpay.merchant.convert.AppConvert;
import com.huiminpay.merchant.entity.App;
import com.huiminpay.merchant.entity.Merchant;
import com.huiminpay.merchant.entity.Store;
import com.huiminpay.merchant.mapper.AppMapper;
import com.huiminpay.merchant.mapper.MerchantMapper;
import com.huiminpay.merchant.mapper.StoreMapper;
import com.yh.merchant.api.AppServiceApi;
import com.yh.merchant.dto.AppDTO;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

/**
 * @Author: HWW
 * @Date: 2021/8/2323:20
 * @Desc:
 */
@Service
public class AppService implements AppServiceApi {

    @Autowired
    private AppMapper appMapper;

    @Autowired
    private MerchantMapper merchantMapper;

    @Autowired
    private StoreMapper storeMapper;

    @Override
    public AppDTO createApp(Long merchantId, AppDTO appDTO) throws BusinessException{
        //校验必要字段
        if(appDTO == null || merchantId==null|| merchantId == 0 || StringUtils.isEmpty(appDTO.getAppName())){
            BusinessCast.cast(CommonErrorCode.E_100101);
        }
        //查看商户是否审核通过，适合通过创建应用
        Merchant merchant = merchantMapper.selectById(merchantId);
        // 审核状态0‐未申请,1‐已申请待审核,2‐审核通过,3‐审核拒绝
        if(merchant == null || !merchant.getAuditStatus().equals("2")){
            BusinessCast.cast(CommonErrorCode.E_200236);
        }

        Integer count = appMapper.selectCount(new LambdaQueryWrapper<App>().eq(App::getAppName, appDTO.getAppName()));
        if(count > 0){
            BusinessCast.cast(CommonErrorCode.E_200234);
        }

        App app = AppConvert.INSTANCE.dto2entity(appDTO);
        app.setMerchantId(merchantId);
        app.setAppId(UUID.randomUUID().toString());
        appMapper.insert(app);
        return AppConvert.INSTANCE.entity2dto(app);

    }

    @Override
    public List<AppDTO> findApps(Long merchantId) {
        List<App> apps = appMapper.selectList(new LambdaQueryWrapper<App>().eq(App::getMerchantId, merchantId));
        return AppConvert.INSTANCE.listentity2dto(apps);
    }

    @Override
    public AppDTO findAppByAppId(String appId) {
        App app = appMapper.selectOne(new QueryWrapper<App>().lambda().eq(App::getAppId, appId));
        return AppConvert.INSTANCE.entity2dto(app);
    }

    @Override
    public Boolean checkStoreApp2Merchant(String appId,Long storeId, Long merchantId) {

        // 查询应用是否属于某个商户
        Integer count1 = appMapper.selectCount(new LambdaQueryWrapper<App>()
                .eq(App::getAppId, appId)
                .eq(App::getMerchantId, merchantId));

        //查询门店是否属于某商户
        Integer count2 = storeMapper.selectCount(new LambdaQueryWrapper<Store>()
                .eq(Store::getId, storeId)
                .eq(Store::getMerchantId, merchantId));
        return count1>0 && count2>0;
    }


}
